﻿<%@ Page Title="" Language="C#" MasterPageFile="~/forumMaster.Master" AutoEventWireup="true" CodeBehind="SubForum.aspx.cs" Inherits="forum.SubForum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

      <script type="text/javascript">
            var threadToUpdate;
            $(document).ready(function () {




                $(".deleteThread").live("click", function () {

                    if (confirm("Delete thread?")) {

                        var parameters;
                        var threadId = $(this).attr("threadId");
                        parameters = "{'threadId':'" + threadId + "'}";
                        $.ajax({
                            type: "POST",
                            url: "../Ajax/forumAjax.asmx/deleteThread",
                            contentType: "application/json; charset=utf-8",
                            data: parameters,
                            success: function (result) {
                                if (result.d != "bad") {
                                    location.reload();
                                    return false;
                                }
                            },
                            error: function () {
                                return false;
                            }
                        });
                    }
                    return false;
                });



                $("#lnkbtnAddThread").click(function () {
                    if ($(".Active :checkbox").attr('checked')) {

                        $("#txtaddThreadTitle").val("");
                        $("#txtAddThreadBody").val("");
                        open($("#addThreadPopUp"));
                    }
                    else {
                        //user not active
                        if (confirm("You are not active. you can be band if you add a thread")) {
                            $("#txtaddThreadTitle").val("");
                            $("#txtAddThreadBody").val("");
                            open($("#addThreadPopUp"));
                        }
                    }
                    return false;
                });


                $("#addThreadPopUp").dialog({
                    autoOpen: false,
                    title: "Add thread",
                    modal: true,
                    buttons: {
                        "ok": function () {

                            var forumId = $("#hdnfldForumId").val();
                            var title = $("#txtaddThreadTitle").val();
                            var body = $("#txtAddThreadBody").val();
                            if (title != "" && body != "") {
                                var parameters = "{'forumId':'" + forumId + "','title': '" + title + "','body': '" + body + "'}";
                                $.ajax({
                                    type: "POST",
                                    url: "../Ajax/forumAjax.asmx/addThread",
                                    contentType: "application/json; charset=utf-8",
                                    data: parameters,
                                    dataType: "json",
                                    success: function (result) {

                                        if (result.d != "bad") {
                                            setTimeout(function () { location.reload(); }, 1000);


                                            return false;
                                        }
                                        else {
                                            alert("went wrong!");
                                            return false;
                                        }
                                    },
                                    error: function () {
                                    }
                                });



                            }
                            else {
                                alert("Must input Title or Body");
                                return false;
                            }




                            $(this).dialog("close");
                            return false;
                        },
                        "cancel": function () {
                            $(this).dialog("close");
                        }
                    }
                });




                $(".editThread").click(function () {
                    $("#txteditThreadTitle").val("");
                    $("#txteditThreadBody").val("");
                    var threadId = $(this).attr("threadId");
                    threadToUpdate = threadId;
                    var parameters = "{'threadId':'" + threadId + "'}";
                    $.ajax({
                        type: "POST",
                        url: "../Ajax/forumAjax.asmx/getThreadDetails",
                        contentType: "application/json; charset=utf-8",
                        data: parameters,
                        dataType: "json",
                        success: function (result) {

                            if (result.d != "bad") {
                                var array = result.d.split(',');
                                var ajaxTitle = array[0];
                                var ajaxBody = array[1];
                                $("#txteditThreadTitle").val(ajaxTitle);
                                $("#txteditThreadBody").val(ajaxBody);

                                return false;
                            }
                            else {
                                alert("user name and password are wrong!");
                                return false;
                            }
                        },
                        error: function () {
                        }
                    });


                    open($("#editThreadPopUp"));
                    return false;
                });


                $("#editThreadPopUp").dialog({
                    autoOpen: false,
                    title: "Edit thread",
                    modal: true,
                    buttons: {
                        "ok": function () {
                            var threadId = threadToUpdate;
                            var title = $("#txteditThreadTitle").val();
                            var body = $("#txteditThreadBody").val();

                            if (title != "" || body != "") {
                                var parameters = "{'threadId':'" + threadId + "','title': '" + title + "','body': '" + body + "'}";
                                $.ajax({
                                    type: "POST",
                                    url: "../Ajax/forumAjax.asmx/editThread",
                                    contentType: "application/json; charset=utf-8",
                                    data: parameters,
                                    dataType: "json",
                                    success: function (result) {

                                        if (result.d != "bad") {

                                            setTimeout(function () { location.reload(); }, 1000);
                                            return false;
                                        }
                                        else {
                                            alert("user name and password are wrong!");
                                            return false;
                                        }
                                    },
                                    error: function () {
                                    }
                                });



                            }
                            else {
                                alert("Must input Title or Body");
                                return false;
                            }




                            $(this).dialog("close");
                            return false;
                        },
                        "cancel": function () {
                            $(this).dialog("close");
                        }
                    }
                });


                $("#linkTest").click(function () {
                    //$('#inner').append('<li>Test</li>');
                    var SubForumId = $("#hdnfldForumId").val();
                    var parameters = "{'SubForumId':'" + SubForumId + "'}";
                    $.ajax({
                        type: "POST",
                        url: "../Ajax/forumAjax.asmx/getNewTitles",
                        contentType: "application/json; charset=utf-8",
                        data: parameters,
                        dataType: "json",
                        success: function (result) {
                            if (result.d != "bad") {
                                var titles = result.d.split('#');
                                for (var i = 0; i < titles.length - 1; i++) {
                                    var threadTitle = titles[i].split(',')[0];
                                    var threadId = titles[i].split(',')[1];
                                    $('#inner').append('<li> <div class"content"><div class="top"><a class="threadLink" href="Thread.aspx?ThreadId=' + threadId + '">' + threadTitle + '</a></div></div> </li>');
                                }
                            }
                            else {
                                return false;
                            }
                        },
                        error: function () {
                        }

                    });
                    return false;
                });



                /*
                $.post("Threads.ashx", ({ SubForumId: subForumId }), function (data) {
                alert(data);
                var ul = $("#divSubForumThreads ul");

                for (var i = 0; i < data.Threads.length; i++) {

                ul.append('<li> <div class"content"><div class="top"><a class="threadLink" href="Thread.aspx?ThreadId=' + data.Thread[i].ThreadId + '">' + data.Thread[i].Title + '</a></div></div> </li>');
                }
                });

                return false;
                });
                */

                /*
                $("#linkTest").click(function () {
                var subForumId = $("#hdnfldForumId").val();

                $.post("Threads.ashx", ({ SubForumId: subForumId }), function (data) {
                alert(data);
                var ul = $("#divSubForumThreads ul");

                for (var i = 0; i < data.Threads.length; i++) {

                ul.append('<li> <div class"content"><div class="top"><a class="threadLink" href="Thread.aspx?ThreadId=' + data.Thread[i].ThreadId + '">' + data.Thread[i].Title + '</a></div></div> </li>');
                }
                });

                return false;
                });
                */

                /* <li>
                <div class="thread">
                <div class="top">
                <asp:Label ID="lblUserName" class="userNameClass" runat="server" Text=""><%# Eval("userName")%></asp:Label> 
                <asp:Label ID="lblCreationDate" class="creationDateClass" runat="server" Text=""><%# Eval("creationDate")%></asp:Label>  
                </div>
                <div class="content">
                <a class="threadLink" href="Thread.aspx?ThreadId=<%# Eval("threadId") %>" >  <%# Eval("threadTitle")%> </a>
                <asp:Label ID="lblContent" class="contentClass" runat="server" Text=""><%# Eval("threadContent")%></asp:Label> 
                </div>
                <asp:HyperLink ID="hyplnkEdit" threadId='<%# Eval("threadId")%>' class="editThread" runat="server">edit</asp:HyperLink>
                <asp:HyperLink ID="lbkbtnDelete" class="deleteThread" threadId='<%# Eval("threadId")%>' runat="server">delete</asp:HyperLink>
                </div>
                </li>
                */


            });
      </script>
       <style type="text/css">
       

       
       .editThread
       {
           color:Blue;
       }
       
       .editThread:hover
       {
           text-decoration: underline;
           cursor:pointer;
       }
       
       .deleteThread
       {
            color:Blue;
       }
       
        .deleteThread:hover
       {
           text-decoration: underline;
           cursor:pointer;
       }
       
       
       .thread
       {
           margin-top:15px;
       }
       
       #divAddThread
       {
           margin-bottom:20px;
           margin-top:40px;
       }
       
       #txtAddThreadBody
       {
           width: 255px; 
           height: 172px;
       }
       
       #txtaddThreadTitle
       {
         display: block;
       }
       
       
       #txteditThreadBody
       {
           width: 255px; 
           height: 172px;
       }
       
       #txteditThreadTitle
       {
         display: block;
       }
       

       
       #hdnfldForumId
       {
           display:none;
       }
       
       #lblSubForumeName
       {
           font-size:30px;
           color:Red;
       }
       #forumName
       {
        margin-top: 10px;
       }
       
       #moderatorsTitle
       {
        margin-top: 10px;
       }
       .horizontal_list
       {
           float:left;
           margin-left: 20px;
       }
       
       
       #moderatorsList
       {
           margin-left: -41px;
       }
       
       .threadLink
       {
           text-decoration:none;
       }
       
       .threadLink:hover
       {
           text-decoration:underline;
       }
 
       

       
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="divSubForumTitle">
           <h3 id="headSubForum">View Sub forum threads</h3>
    </div>
    <div id="forumName">
            <asp:Label ID="lblSubForumeName" ClientIDMode="Static" runat="server" Text=""></asp:Label>
    </div>
    <div id ="moderatorsTitle">
        <asp:Label ID="lblModerators" ClientIDMode="Static" runat="server" Text="Sub forum moderators are: "></asp:Label>
    </div>
    <div class="divSubForumModerators">
            <asp:ListView ID="lvSubForumModerators" runat="server">
                <EmptyDataTemplate>
                    <span>no moderators</span>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <li class="horizontal_list">
                        <span ><%# Eval("userName")%></span>
                    </li>
                </ItemTemplate>
                <LayoutTemplate>
                    <ul id="moderatorsList">
                        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                    </ul>
                </LayoutTemplate>
            </asp:ListView>
   </div>

   <div id="divAddThread">
       <asp:LinkButton ID="lnkbtnAddThread" ClientIDMode="Static" runat="server">Add Thread</asp:LinkButton>
   </div>


   <asp:Label ID="lblThreads" runat="server" Text="Sub forum threads:"></asp:Label>
    <div class="divSubForumThreads">
            <asp:ListView ID="lvThreads" runat="server" OnItemDataBound="lvThreads_ItemDataBound">
                <EmptyDataTemplate>
                    <span>Sub forum is empty</span>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <li>
                        <div class="thread">
                            <div class="top">
                                <asp:Label ID="lblUserName" class="userNameClass" runat="server" Text=""><%# Eval("userName")%></asp:Label> 
                                <asp:Label ID="lblCreationDate" class="creationDateClass" runat="server" Text=""><%# Eval("creationDate")%></asp:Label>  
                            </div>
                            <div class="content">
                               <a class="threadLink" href="Thread.aspx?ThreadId=<%# Eval("threadId") %>" >  <%# Eval("threadTitle")%> </a>
                               <asp:Label ID="lblContent" class="contentClass" runat="server" Text=""><%# Eval("threadContent")%></asp:Label> 
                            </div>
                            <asp:HyperLink ID="hyplnkEdit" threadId='<%# Eval("threadId")%>' class="editThread" runat="server">edit</asp:HyperLink>
                            <asp:HyperLink ID="lbkbtnDelete" class="deleteThread" threadId='<%# Eval("threadId")%>' runat="server">delete</asp:HyperLink>
                        </div>
                    </li>
                </ItemTemplate>
                <LayoutTemplate>
                    <ul id="inner">
                        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                    </ul>
                </LayoutTemplate>
            </asp:ListView>
   </div>


   <div id="addThreadPopUp">

            <asp:Label ID="lbladdThreadTitle" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Title"></asp:Label>
            <asp:TextBox ID="txtaddThreadTitle" ClientIDMode="Static" runat="server" ></asp:TextBox>
            <asp:Label ID="lbladdThreadBody" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Body"></asp:Label>
            <textarea id="txtAddThreadBody" rows="5" cols="5"></textarea>
   </div>

    <div id="editThreadPopUp">

            <asp:Label ID="lbleditThreadTitle" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Title"></asp:Label>
            <asp:TextBox ID="txteditThreadTitle" ClientIDMode="Static" runat="server" ></asp:TextBox>
            <asp:Label ID="lbleditThreadBody" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Body"></asp:Label>
            <textarea id="txteditThreadBody" rows="5" cols="5"></textarea>
   </div>


    <asp:HiddenField ID="hdnfldForumId"  ClientIDMode="Static" runat="server" />

</asp:Content>
